<template>
  <div class="dashboard-container">
    <div class="app-container">
      <!-- 卡片 -->
      <el-card class="box-card">
        <!-- tabs 标签 -->
        <!-- v-model name属性 标签高亮 -->
        <el-tabs v-model="activeName">
          <el-tab-pane class="tab-pane" label="组织架构" name="first">
            <!-- 标签页内容 -->
            <!-- 使用 Layout 布局绘制头部区域 -->
            <el-row type="flex" justify="space-between" align="middle" class="department-header">
              <!-- 左侧公司名 -->
              <!-- span 占多少列 -->
              <el-col :span="20">
                <i class="el-icon-s-home" />
                <span class="company">江苏传智播客教育科技股份有限公司</span>
              </el-col>
              <!-- 右侧 -->
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <el-col>负责人</el-col>
                  <el-col>
                    <el-dropdown>
                      <span class="el-dropdown-link">
                        操作<i class="el-icon-arrow-down el-icon--right" />
                      </span>
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item @click.native="addDept('')">添加子部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
            <!-- 树形结构 -->
            <!-- default-expand-all 默认展开所有 -->
            <el-tree default-expand-all :data="treeData" :props="defaultProps" class="departments-tree" @node-click="handleNodeClick">
              <!-- 右侧操作栏 插槽-->
              <template slot-scope="{ data }">
                <el-row type="flex" justify="space-between" style="height: 50px; width: 100%;" align="middle">
                  <el-col :span="20">
                    <span>{{ data.name }}</span>
                  </el-col>
                  <el-col :span="4">
                    <el-row type="flex" justify="end">
                      <el-col>{{ data.manager }}</el-col>
                      <el-col>
                        <el-dropdown>
                          <span class="el-dropdown-link">
                            操作<i class="el-icon-arrow-down el-icon--right" />
                          </span>
                          <el-dropdown-menu slot="dropdown">
                            <el-dropdown-item @click.native="addDept(data.id)">添加子部门</el-dropdown-item>
                            <el-dropdown-item @click.native="editDept(data.id)">编辑部门</el-dropdown-item>
                            <el-dropdown-item v-if="data&&!data.children" @click.native="delDept(data.id)">删除部门</el-dropdown-item>
                          </el-dropdown-menu>
                        </el-dropdown>
                      </el-col>
                    </el-row>
                  </el-col>
                </el-row>
              </template>
            </el-tree>
          </el-tab-pane>
        </el-tabs>

        <!-- 添加/编辑子部门弹框 -->
        <el-dialog
          :title="isEdit?'编辑部门':'添加子部门'"
          :visible.sync="showDialog"
          width="50%"
          :close-on-click-modal="false"
          :close-on-press-escape="false"
        >
          <!-- 弹框表单组件 -->
          <dialog-form
            v-if="showDialog"
            :parent-id="parentId"
            :is-edit="isEdit"
            :origin-list="originList"
            @close="updateDepartment"
            @cancel="showDialog=$event"
          />
          <!-- 底部按钮区域 -->
          <!-- <span slot="footer" class="dialog-footer">
            <el-button @click="showDialog = false">取 消</el-button>
            <el-button type="primary" @click="showDialog = false">确 定</el-button>
          </span> -->
        </el-dialog>
      </el-card>
    </div>
  </div>
</template>

<script>
import { getDepartments, delDepartment } from '@/api/department'
import { transTree } from '@/utils/index'
import dialogForm from './dialogForm.vue'
export default {
  name: 'Departments',
  components: {
    dialogForm
  },
  data() {
    return {
      // tab 高亮
      activeName: 'first',
      // 树形结构数据
      treeData: [
        { name: '财务', children: [
          { name: '财务1' },
          { name: '财务2' }
        ] }
      ],
      // 渲染依据字段
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      // 控制弹框的显隐
      showDialog: false,
      // 父级id
      parentId: '',
      // 是否是编辑
      isEdit: false,
      // 全部部门信息
      originList: []
    }
  },
  created() {
    this.getDepartments()
  },
  methods: {
    // 删除部门
    async delDept(id) {
      // console.log(id)
      const result = await this.$confirm('确定是否删除?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => err)
      // console.log(result)
      // 点击取消
      if (result === 'cancel') return
      // 点击确定
      const res = await delDepartment(id)
      // console.log(res)
      // 更新视图
      this.getDepartments()
      // 提示
      this.$message.success(res.message)
    },

    // 编辑部门
    editDept(id) {
      // console.log(data)
      // 设置id
      this.parentId = id
      // 是编辑状态
      this.isEdit = true
      // 显示弹框
      this.showDialog = true
    },

    // 更新树形结构
    updateDepartment() {
      // 关闭弹框
      this.showDialog = false
      // 更新树形视图
      this.getDepartments()
    },

    // 获取父级id
    addDept(id) {
      // 显示弹框
      this.showDialog = true
      // 不是编辑
      this.isEdit = false
      // console.log(id)
      this.parentId = id
    },

    // 获取公司组织架构数据
    async getDepartments() {
      const res = await getDepartments()
      // console.log(res)
      // 子组件自定义校验规则 需要用到扁平化的数据
      const originList = res.data.depts.map(item => ({
        id: item.id,
        pid: item.pid,
        name: item.name,
        code: item.code
      }))
      // console.log(originList)
      this.originList = originList
      this.treeData = transTree(res.data.depts, '')
    },

    // 点击节点
    handleNodeClick() {}
  }
}
</script>

<style lang="scss" scoped>
.box-card {
  padding: 10px 30px;
}
.tab-pane {
  padding: 20px 80px;

  .company {
    margin-left: 12px;
    font-weight: bold;
    font-size: 14px;
  }

  .department-header {
    height: 50px;
    display: flex;
    align-items: center;
    border-bottom: 1px solid #ccc;
  }
}
.departments-tree {
  margin: 12px 0px;
}
.el-tree {
  margin-top: 10px;
}
</style>
